package com.lanou.tipsy.fragment.singer.detail;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.singer.AlbumsAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.singer.singers_album.SAlbumData;
import com.lanou.tipsy.entity.singer.singers_album.SAlbumsRoot;
import com.lanou.tipsy.tools.listview.NoScrollGridView;
import com.lanou.tipsy.tools.volley.ResponseErrorListener;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;


/**
 * Created by FengJun on 15/10/22.
 * 歌手-歌手详情-专辑
 */
public class SDetailAlbumFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    public static final String TAG = "SDetailAlbumFragment";
    private NoScrollGridView gv_album;
    private AlbumsAdapter adapter;
    private ArrayList<SAlbumData> datas;
    private VolleySingleton volleySingleton;
    private ResponseErrorListener errorListener;
    private int urlId = 50530;
    private StringRequest request;
    private Context context;
    private FragmentManager manager;

    public SDetailAlbumFragment() {
        super();
    }

    public SDetailAlbumFragment(int urlId) {
        this();
        this.urlId = urlId;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_singer_det_album, null);
        manager = getActivity().getSupportFragmentManager();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //初始化控件
        initView(getView());
        //解析
        analysis();
    }

    private void initView(View view) {
        gv_album = (NoScrollGridView) view.findViewById(R.id.gv_album);
        volleySingleton = VolleySingleton.getInstance();
        errorListener = new ResponseErrorListener();
        gv_album.setOnItemClickListener(this);
    }

    private void analysis() {
        String url = Constants.SINGER_ALBUM_START + urlId + Constants.SINGER_ALBUM_END;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                SAlbumsRoot root = JSON.parseObject(response, SAlbumsRoot.class);
                datas = root.getData();
                adapter = new AlbumsAdapter(datas, context);
                gv_album.setAdapter(adapter);
            }
        }, errorListener);
        volleySingleton.addQueue(request, TAG);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SAlbumData data = (SAlbumData) parent.getItemAtPosition(position);
        SDetailAlbumDetailFragment fragment = new SDetailAlbumDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("albums", data);
        fragment.setArguments(bundle);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.addToBackStack(null);
        transaction.replace(R.id.fl_main, fragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.commit();
    }

    @Override
    public void onStop() {
        super.onStop();
        volleySingleton.removeRequest(TAG);
    }
}
